var app = getApp()
var getAppGlobalData = require('../../templates/copyright/copyright.js');
Page({
  data: {
    tabList: [{ status: 'time', name: '时间' }, { status: 'behavior', name: '行为' }],
    currentIndex: 0,
    currentTab: 'time',
    windowHeight: '',
    more: true,
    loading: false,
    isEmpty: false,
    show: false,
    authStatus: true,
    radarTime: '',
    timeList: [],
    page: 1,
    more: true,
    loading: false,
    isEmpty: false,
    show: false,
    tmpDDDDDDataLength: 0,
    behaviorInfo: [],
    behaviorList: [],
    globalData: {},
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数 
    var that = this;
    wx.hideShareMenu();
    getAppGlobalData.getAppGlobalData(that);
  },
  onReady: function () {
    // console.log("页面渲染完成")
  },
  onShow: function () {
    // 页面显示 
    var that = this;
    // var date = new app.util.date();
    // var radarTime = date.dateToLong(new Date);
    // that.setData({
    //   radarTime: (radarTime / 1000).toFixed(0),
    //   currentTab: 'time',
    // }) 
    if (that.data.currentTab == 'time') {
      console.log("时间")
      that.getRadarByTime();
    } else if (that.data.currentTab == 'behavior') {
      console.log("行为")
      that.getRadarByBehaviorInfo();
      that.getRadarByBehavior();
    }
    that.setData({
      windowHeight: wx.getSystemInfoSync().windowHeight
    })
  },
  onHide: function () {
    // console.log("页面隐藏")
  },
  onUnload: function () {
    // console.log("页面关闭")
  },
  onPullDownRefresh: function () {
    // console.log("监听用户下拉动作") 
    var that = this;


    // var date = new app.util.date();
    // var radarTime = date.dateToLong(new Date);
    if (that.data.currentTab == 'time') {

      that.setData({
        onPullDownRefresh: true,
        page: 1,
        more: true,
        loading: false,
        isEmpty: false,
        show: false,
      })


      // that.setData({
      //   radarTime: (radarTime / 1000).toFixed(0),
      //   timeList: [],
      //   tmpDDDDDDataLength: 0
      // })
      that.getRadarByTime();
    } else if (that.data.currentTab == 'behavior') {
      that.setData({
        behaviorInfo: {},
        behaviorList: {}
      })
      that.getRadarByBehaviorInfo();
      that.getRadarByBehavior();
    }
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom: function () {
    // console.log("监听页面上拉触底") 
    var that = this;
    if (that.data.currentTab == 'time') {

      that.setData({
        show: true
      })
      if (that.data.isEmpty == false) {
        that.setData({
          page: that.data.page + 1
        })
        that.getRadarByTime();
      }


      // that.setData({
      //   show: true
      // })
      // if (that.data.isEmpty == false) {
      //   that.setData({
      //     'radarTime': that.data.radarTime - (24 * 60 * 60)
      //   })
      //   that.getRadarByTime();
      // }
    }
  },
  onPageScroll: function (e) {
    //console.log("监听页面滚动", e);
  },
  onShareAppMessage: function (res) {
    // console.log("用户点击右上角分_享")
  },
  getRadarByTime: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/aiTime',
      'cachetime': '30',
      // 'showLoading': false,
      'method': 'POST',
      'data': {
        uniacid: app.siteInfo.uniacid,
        page: that.data.page
        // date: that.data.radarTime
      },
      success: function (res) {
        console.log("entry/wxapp/aiTime ==>", res)
        if (!res.data.errno) {

          var tmpData = res.data.data.list;
          if (tmpData.length == 0) {
            that.setData({
              more: false,
              loading: false,
              isEmpty: true,
              show: true
            })
            return false;
          }
          that.setData({
            loading: true
          })

          var onPullDownRefresh = that.data.onPullDownRefresh;
          if (onPullDownRefresh == true) {
            that.setData({
              timeList: []
            })
          }
          that.setData({
            onPullDownRefresh: false
          })
          var tmpListData = that.data.timeList;

          // var tmpDataList = [];
          var date = new app.util.date();
          // if (!tmpData[0].create_time) {
          //   tmpRadarDate = '';
          // }
          // if (tmpData[0].create_time && tmpData[0].create_time.length < 12) {
          //   tmpRadarDate = (date.dateToStr('yyyy年MM月DD日', date.longToDate(tmpData[0].create_time * 1000)));
          // }

          for (let i in tmpData) {
            if (tmpData[i].create_time && tmpData[i].create_time.length < 12) {
              tmpData[i].create_time = date.dateToStr('MM-DD HH:mm', date.longToDate(tmpData[i].create_time * 1000));
            }

            if (tmpData[i].sign == 'praise') {
              if (tmpData[i].type == 2) {
                if (tmpData[i].count == 1) {
                  tmpData[i].countText = '，沟通从此刻开始。'
                }
                if (tmpData[i].count == 2) {
                  tmpData[i].countText = '，把握深度交流的机会。'
                }
                if (tmpData[i].count == 3) {
                  tmpData[i].countText = '，建议标注为重点客户'
                }
                if (tmpData[i].count > 3) {
                  tmpData[i].countText = '，成交在望哦。'
                }
              }
            }

            tmpListData.push(tmpData[i]);
            // tmpDataList.push(tmpData[i]);
          }


          // tmpListData.push({ time: tmpRadarDate, list: tmpDataList });
          // tmpListData.push({ time: 0, list: tmpDataList });

          // let tmpListDataDDDDDDDD = tmpListData;
          // for (let j in tmpListDataDDDDDDDD) {
          //   if (!tmpListDataDDDDDDDD[j].time) {
          //     tmpListDataDDDDDDDD.splice(j, 1)
          //   }
          // }


          that.setData({
            // timeList: tmpListDataDDDDDDDD
            timeList: tmpListData
          })
          // console.log(tmpListData)

        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getRadarByBehaviorInfo: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/aiBehaviorHeader',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        uniacid: app.siteInfo.uniacid,
      },
      success: function (res) {
        // console.log("entry/wxapp/aiBehaviorHeader ==>", res)
        if (!res.data.errno) {
          that.setData({
            behaviorInfo: res.data.data
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getRadarByBehavior: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/aiBehaviorOther',
      'cachetime': '30',
      'method': 'POST',
      'data': {
      },
      success: function (res) {
        // console.log("entry/wxapp/aiBehaviorOther ==>", res)
        if (!res.data.errno) {
          that.setData({
            behaviorList: res.data.data
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toJump: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.status;
    var id = e.currentTarget.dataset.id;
    if (status == 'toCustomInfo') {
      console.log("跳转至客户页面")
      wx.navigateTo({
        url: '/longbing_card/staff/custom/detail/detail?id=' + id 
      })
    }
  },
  formSubmit: function (e) {
    var that = this;
    var formId = e.detail.formId;
    var index = e.detail.target.dataset.index;
    var status = e.detail.target.dataset.status;
    that.toSaveFormIds(formId);
    if (status != 'toHome') {
      that.setData({
        currentIndex: index,
        currentTab: status,
      })
    }
    if (status == 'time') {
      console.log("时间")
      if (that.data.timeList.length == 0) {
        that.getRadarByTime();
      }
    } else if (status == 'behavior') {
      console.log("行为")
      if (that.data.behaviorInfo.length == 0) {
        that.getRadarByBehaviorInfo();
      }
      if (that.data.behaviorList.length == 0) {
        that.getRadarByBehavior();
      }
    } else if (status == 'toHome') {
      console.log("返回首页")
      wx.navigateTo({
        url: '/longbing_card/pages/index/index?to_uid=' + wx.getStorageSync("userid") + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toCard'
      })
    }
  },
  toSaveFormIds: function (formId) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/formid',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        formId: formId
      },
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  }
})